﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;

using V5_DBUtility;
using V5_Model;
namespace V5_DAL.SqlServer {
    public class DALDiyPage {

        #region  BaseMethod
        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId() {
            return DbHelperSQL.GetMaxID("ID", "S_DiyPage");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from S_DiyPage");
            strSql.Append(" where ID=" + ID);
            return DbHelperSQL.Exists(strSql.ToString());
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(ModelDiyPage model) {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.ClassID != null) {
                strSql1.Append("ClassID,");
                strSql2.Append("" + model.ClassID + ",");
            }
            if (model.DiyName != null) {
                strSql1.Append("DiyName,");
                strSql2.Append("'" + model.DiyName + "',");
            }
            if (model.ID != null) {
                strSql1.Append("ID,");
                strSql2.Append("" + model.ID + ",");
            }
            if (model.IsOutHtml != null) {
                strSql1.Append("IsOutHtml,");
                strSql2.Append("" + model.IsOutHtml + ",");
            }
            if (model.IsTemplate != null) {
                strSql1.Append("IsTemplate,");
                strSql2.Append("" + model.IsTemplate + ",");
            }
            if (model.PageContent != null) {
                strSql1.Append("PageContent,");
                strSql2.Append("'" + model.PageContent + "',");
            }
            if (model.PageDescription != null) {
                strSql1.Append("PageDescription,");
                strSql2.Append("'" + model.PageDescription + "',");
            }
            if (model.PageKeyWords != null) {
                strSql1.Append("PageKeyWords,");
                strSql2.Append("'" + model.PageKeyWords + "',");
            }
            if (model.PagePath != null) {
                strSql1.Append("PagePath,");
                strSql2.Append("'" + model.PagePath + "',");
            }
            if (model.PageTitle != null) {
                strSql1.Append("PageTitle,");
                strSql2.Append("'" + model.PageTitle + "',");
            }
            if (model.ParentID != null) {
                strSql1.Append("ParentID,");
                strSql2.Append("" + model.ParentID + ",");
            }
            if (model.Template != null) {
                strSql1.Append("Template,");
                strSql2.Append("'" + model.Template + "',");
            }
            strSql.Append("insert into S_DiyPage(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ModelDiyPage model) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update S_DiyPage set ");
            if (model.ClassID != null) {
                strSql.Append("ClassID=" + model.ClassID + ",");
            }
            if (model.DiyName != null) {
                strSql.Append("DiyName='" + model.DiyName + "',");
            }
            if (model.IsOutHtml != null) {
                strSql.Append("IsOutHtml=" + model.IsOutHtml + ",");
            }
            if (model.IsTemplate != null) {
                strSql.Append("IsTemplate=" + model.IsTemplate + ",");
            }
            if (model.PageContent != null) {
                strSql.Append("PageContent='" + model.PageContent + "',");
            }
            if (model.PageDescription != null) {
                strSql.Append("PageDescription='" + model.PageDescription + "',");
            }
            if (model.PageKeyWords != null) {
                strSql.Append("PageKeyWords='" + model.PageKeyWords + "',");
            }
            if (model.PagePath != null) {
                strSql.Append("PagePath='" + model.PagePath + "',");
            }
            if (model.PageTitle != null) {
                strSql.Append("PageTitle='" + model.PageTitle + "',");
            }
            if (model.ParentID != null) {
                strSql.Append("ParentID=" + model.ParentID + ",");
            }
            if (model.Template != null) {
                strSql.Append("Template='" + model.Template + "',");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ID=" + model.ID + " ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from S_DiyPage ");
            strSql.Append(" where ID=" + ID);
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ModelDiyPage GetModel(int ID) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from S_DiyPage ");
            strSql.Append(" where ID=" + ID);
            ModelDiyPage model = new ModelDiyPage();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds != null && ds.Tables[0].Rows.Count > 0) {
                if (ds.Tables[0].Rows[0]["ClassID"] != null && ds.Tables[0].Rows[0]["ClassID"].ToString() != "") {
                    model.ClassID = int.Parse(ds.Tables[0].Rows[0]["ClassID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DiyName"] != null && ds.Tables[0].Rows[0]["DiyName"].ToString() != "") {
                    model.DiyName = ds.Tables[0].Rows[0]["DiyName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ID"] != null && ds.Tables[0].Rows[0]["ID"].ToString() != "") {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsOutHtml"] != null && ds.Tables[0].Rows[0]["IsOutHtml"].ToString() != "") {
                    model.IsOutHtml = int.Parse(ds.Tables[0].Rows[0]["IsOutHtml"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsTemplate"] != null && ds.Tables[0].Rows[0]["IsTemplate"].ToString() != "") {
                    model.IsTemplate = int.Parse(ds.Tables[0].Rows[0]["IsTemplate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["PageContent"] != null && ds.Tables[0].Rows[0]["PageContent"].ToString() != "") {
                    model.PageContent = ds.Tables[0].Rows[0]["PageContent"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PageDescription"] != null && ds.Tables[0].Rows[0]["PageDescription"].ToString() != "") {
                    model.PageDescription = ds.Tables[0].Rows[0]["PageDescription"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PageKeyWords"] != null && ds.Tables[0].Rows[0]["PageKeyWords"].ToString() != "") {
                    model.PageKeyWords = ds.Tables[0].Rows[0]["PageKeyWords"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PagePath"] != null && ds.Tables[0].Rows[0]["PagePath"].ToString() != "") {
                    model.PagePath = ds.Tables[0].Rows[0]["PagePath"].ToString();
                }
                if (ds.Tables[0].Rows[0]["PageTitle"] != null && ds.Tables[0].Rows[0]["PageTitle"].ToString() != "") {
                    model.PageTitle = ds.Tables[0].Rows[0]["PageTitle"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ParentID"] != null && ds.Tables[0].Rows[0]["ParentID"].ToString() != "") {
                    model.ParentID = int.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Template"] != null && ds.Tables[0].Rows[0]["Template"].ToString() != "") {
                    model.Template = ds.Tables[0].Rows[0]["Template"].ToString();
                }
                return model;
            }
            else {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM S_DiyPage ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where  1=1 " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  Method

        #region S_DiyPage开始
        public bool DeleteList(string IDlist) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from S_DiyPage ");
            strSql.Append(" where ID in (" + IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0) {
                return true;
            }
            else {
                return false;
            }
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder) {
            return this.GetList("*", Top, strWhere, filedOrder);
        }

        public DataSet GetList(int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            return this.GetList("*", PageSize, PageIndex, strWhere, strOrder, ref RecordCount);
        }

        public DataSet GetList(string strFiled, string strWhere) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select " + strFiled + " ");
            strSql.Append(" FROM S_DiyPage ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where 1=1 " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetList(string strFiled, int Top, string strWhere, string filedOrder) {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0) {
                strSql.Append(" Top " + Top.ToString());
            }
            strSql.Append(" " + strFiled + " ");
            strSql.Append(" FROM S_DiyPage ");
            if (strWhere.Trim() != "") {
                strSql.Append(" where 1=1 " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetList(string strFiled, int PageSize, int PageIndex, string strWhere, string strOrder, ref int RecordCount) {
            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.Append("Select Count(*) as ID From S_DiyPage ");
            if (strWhere.Trim() != "") {
                strSqlCount.Append(" where 1=1 " + strWhere);
            }
            RecordCount = Convert.ToInt32("0" + DbHelperSQL.GetSingle(strSqlCount.ToString()));
            if (PageIndex <= 1) {
                PageIndex = 1;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Top " + PageSize + " " + strFiled + " from S_DiyPage where 1=1 ");
            if (strWhere.Trim() != "") {
                strSql.Append(" " + strWhere);
            }
            if (PageIndex > 1) {
                strSql.Append(" And ID Not In (Select Top " + ((PageIndex - 1) * PageSize) + " ID From S_DiyPage Where 1=1 ");
                if (strWhere.Trim() != "") {
                    strSql.Append(" " + strWhere);
                }
                if (!string.IsNullOrEmpty(strOrder)) {
                    strSql.Append(" Order by " + strOrder);
                }
                strSql.Append(")");
            }
            if (!string.IsNullOrEmpty(strOrder)) {
                strSql.Append(" Order by " + strOrder);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion S_DiyPage结束
    
    




    }
}